package com.sxjh.config;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.core.util.IdUtil;
import com.sxjh.common.Constant;
import com.sxjh.entity.param.ContractCacheEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 保存合同单缓存
 */
public class ContractCacheConfig {

  private static Cache<String, ContractCacheEntity> cache = CacheUtil.newTimedCache(Constant.MINUTE * 10);

  private ContractCacheConfig() {}

  public static Cache<String, ContractCacheEntity> getCache() {
    return cache;
  }

  /**
   * 创建一个保存合同的缓存
   */
  public static String createCache(ContractCacheEntity entity) {
    String simpleUUID = IdUtil.simpleUUID();
    entity.setUuid(simpleUUID);
    cache.put(simpleUUID, entity);
    return simpleUUID;
  }

  /**
   * 获取该缓存下所有合同号
   */
  public static List<String> getAllContract() {
    List<String> list = new ArrayList<>();
    cache.forEach(item -> {
      list.add(item.getStartContract());
      list.add(item.getEndContract());
    });
    return list;
  }

  /**
   * 获取缓存
   */
  public static ContractCacheEntity getContract(String id) {
    return cache.get(id);
  }

  /**
   * 删除缓存
   */
  public static void remove(String id) {
    cache.remove(id);
  }

}
